library(gapminder)
library(tidyverse)
library(hablar)
library(tsibble)
library(dplyr)
surveyone <- read.csv( "~/Desktop/Political_factors_Jan9.csv" , sep=",")
survey1 <- data.frame(surveyone)
survey1 %>%
DT::datatable(survey1)
# loaded data set for survey one
#This chunk contains the coding schemes of various scales used in survey one: eco-political factors, kahan scale and acceptance scale
codedsurvey1 <- survey1 %>%
#remove row 1
filter(!row_number() %in% c(1)) %>%
# replace risky likert scale with numbers
mutate_at(vars(starts_with("Risky")), funs(case_when(. =="Not at all risky" ~ 1,
. =="Slightly risky" ~ 2,
. =="Moderately risky" ~ 3,
. =="Very risky" ~ 4,
. =="Extremely risky" ~ 5))) %>%
# replace beneficial likert scale with numbers
mutate_at(vars(starts_with("Ben")), funs(case_when(. =="Not at all beneficial" ~ 1,
. =="Slightly beneficial" ~ 2,
. =="Moderately beneficial" ~ 3,
. =="Very beneficial" ~ 4,
. =="Extremely beneficial" ~ 5 ))) %>%
# replace nuclear acceptance likert scale with numbers
mutate_at(vars(N_accept,N_reluctantlyaccept,N_reject), funs(case_when(. == "Strongly disagree" ~ 1,
. == "Somewhat disagree" ~ 2,
. == "Neither agree nor disagree" ~3,
. == "Somewhat agree" ~ 4,
. == "Strongly agree" ~ 5))) %>%
# code likert scale for variables for Kahan scale into numbers
mutate_at(vars(starts_with (c("K_I","K_H","DISPLACE", "POLLUTE", "HEALTH", "JOBS", "BEAUTY", "PRIDE", "NPRIDE", "DEV", "PROSPER", "RELY"))), funs(case_when(. == "Strongly disagree" ~ 1,
. == "Somewhat disagree" ~ 2,
. == "Neither agree nor disagree" ~3,
. == "Somewhat agree" ~ 4,
. == "Strongly agree" ~ 5))) %>%
# reverse code for likert scale for variables for Kahan scale into numbers
mutate_at(vars(starts_with (c("K_S","K_E"))), funs(case_when(. == "Strongly disagree" ~ 5,
. == "Somewhat disagree" ~ 4,
. == "Neither agree nor disagree" ~3,
. == "Somewhat agree" ~ 2,
. == "Strongly agree" ~ 1))) %>%
# code eco-pol scale variables into numbers
mutate_at(vars(SYSTEMDEMO,SYSTEMRELIGION,SYSTEMTECHNO,SYSTEMTOTAL,WEALTHLIM,MECHANISATION,DECISIONDECEN,INDUSTRYLARGE,ECONOMYGLOBAL,OWNERPVT,OWNERNOREG), funs(case_when(. == "Strongly disagree" ~ 1,
. == "Somewhat disagree" ~ 2,
. == "Neither agree nor disagree" ~3,
. == "Somewhat agree" ~ 4,
. == "Strongly agree" ~ 5))) %>%
# reverse code eco-pol scale variables into numbers
mutate_at(vars(DECISIONCEN, INDUSTRYSMALL, ECONOMYLOCAL, ENVOVERDEV,OWNERPUB, OWNERREG), funs(case_when(. == "Strongly disagree" ~ 5,
. == "Somewhat disagree" ~ 4,
. == "Neither agree nor disagree" ~3,
. == "Somewhat agree" ~ 2,
. == "Strongly agree" ~ 1)))
## Warning: `funs()` was deprecated in dplyr 0.8.0.
## Please use a list of either functions or lambdas:
##
## # Simple named list:
## list(mean = mean, median = median)
##
## # Auto named with `tibble::lst()`:
## tibble::lst(mean, median)
##
## # Using lambdas
## list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.
DT::datatable(head(codedsurvey1), class= 'cell-border stripe')
#change age to ordinal variables and wrap as tibble? more accessible on html format
codedsurvey1 %>%
summarize_at(vars(starts_with(c("RISKY"))), list(~mean(., na.rm = TRUE), ~median(., na.rm = TRUE), ~sd(.,na.rm = TRUE),~var(.,na.rm = TRUE), ~n())) %>%
#add row index so later spreading indexed correctly
add_rownames()%>%
#melt to long format
gather(technology, value, -rowname) %>%
# separate risky from variable suffix
separate(technology, c("Risky", "var"), extra = "merge", fill = "left") %>%
#separate mean from variable prefix
separate(var, c("technology", "summary")) %>%
# spread summary values back to wide form
spread(summary,value) %>%
#clean up
select(-rowname, -Risky) %>%
arrange(mean)
## Warning: `add_rownames()` was deprecated in dplyr 1.0.0.
## Please use `tibble::rownames_to_column()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.
## # A tibble: 15 × 6
## technology mean median n sd var
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Solar 1.89 1 904 1.10 1.22
## 2 INDSolar 1.91 1 904 1.11 1.24
## 3 Wind 2.25 2 904 1.17 1.36
## 4 INDFirewoodetc 2.25 2 904 1.10 1.21
## 5 INDHydro 2.31 2 904 1.11 1.23
## 6 INDWind 2.37 2 904 1.15 1.31
## 7 Hydro 2.37 2 904 1.20 1.44
## 8 INDDiesel 2.5 2 904 1.25 1.56
## 9 INDKerosene 2.63 2 904 1.22 1.49
## 10 INDBiogas 2.78 3 904 1.12 1.25
## 11 Oil 2.90 3 904 1.21 1.47
## 12 Coal 3.28 3 904 1.00 1.01
## 13 Gas 3.30 3 904 1.08 1.16
## 14 INDLPG 3.33 3 904 1.13 1.28
## 15 Nuclear 3.46 4 904 1.18 1.39
#round off to two decimal places
codedsurvey1 %>%
summarize_at(vars(starts_with(c("Ben"))), list(~mean(., na.rm = TRUE), ~median(., na.rm = TRUE), ~sd(.,na.rm = TRUE),~var(.,na.rm = TRUE), ~n())) %>%
#add row index so later spreading indexed correctly
add_rownames()%>%
#melt to long format
gather(technology, value, -rowname) %>%
# separate risky from variable suffix
separate(technology, c("Ben", "var"), extra = "merge", fill = "left") %>%
#separate mean from variable prefix
separate(var, c("technology", "summary")) %>%
# spread summary values back to wide form
spread(summary,value) %>%
#clean up
select(-rowname, -Ben) %>%
arrange(mean)
## # A tibble: 15 × 6
## technology mean median n sd var
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 INDFirewoodetc 3.02 3 904 1.03 1.07
## 2 INDKerosene 3.08 3 904 1.03 1.06
## 3 INDDiesel 3.24 3 904 1.01 1.01
## 4 Nuclear 3.25 3 904 1.15 1.32
## 5 Oil 3.27 3 904 1.07 1.14
## 6 INDHydro 3.31 4 904 0.990 0.980
## 7 Coal 3.34 3 904 1.09 1.19
## 8 INDBiogas 3.37 3 904 0.997 0.994
## 9 INDWind 3.38 4 904 0.904 0.818
## 10 Gas 3.39 3 904 1.04 1.08
## 11 Hydro 3.42 4 904 1.00 1.01
## 12 Wind 3.51 4 904 0.965 0.932
## 13 INDLPG 3.53 4 904 1.02 1.05
## 14 INDSolar 3.76 4 904 0.951 0.904
## 15 Solar 3.79 4 904 0.929 0.863
#round off to two decimal places